package org.sselab.leetcode;

/**
 * 我这种算法的问题在于空间占用太大,当数据大于内存时就不可用了!
 *
 * @author pinker on 2017/11/23
 */
public class MedianNum {
  public double findMedianSortedArrays(int[] nums1, int[] nums2) {
    int[] num = new int[nums1.length + nums2.length];
    int index1 = 0, index2 = 0, index = 0;
    while (index1 < nums1.length && index2 < nums2.length) {
      num[index++] = nums1[index1] < nums2[index2] ? nums1[index1++] : nums2[index2++];
    }
    while (index1 < nums1.length) {
      num[index++] = nums1[index1++];
    }
    while (index2 < nums2.length) {
      num[index++] = nums2[index2++];
    }
    for (int i = 0; i < num.length; i++) {
      System.out.print(num[i] + "\t");
    }
    System.out.println();
    return index % 2 == 0 ? (num[index / 2] + num[index / 2 - 1]) * 1.0 / 2 : num[index / 2];
  }
}
